﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace TimeSheet
{
    public class NTimeSheet
    {
        public static List<TimesheetEntries> ConsultarLista(string idUsuario)
        {
            using (var db = new DBTfs_Timesheet())
            {
                var tarefas = db.TimesheetEntries.Where(c => c.sid == idUsuario).ToList();

                var tarefasAgrupadas = tarefas
                    .GroupBy(x => new
                    {
                        x.workItemId,
                        x.sid,
                        x.NomeTarefa,
                        x.TempoNoTFS,
                        x.TempoNaTimeSheet
                    })
                    .Select(y => new TimesheetEntries
                    {
                        workItemId = y.Key.workItemId,
                        NomeTarefa = y.Key.NomeTarefa,
                        TempoNoTFS = y.Key.TempoNoTFS,
                        TempoNaTimeSheet = y.Key.TempoNaTimeSheet,
                        sid = y.Key.sid,
                        hours = y.Sum(c => c.hours)
                    }).ToList();

                foreach (var tarefa in tarefasAgrupadas)
                {
                    var tarefaDoUsuario = db.TimesheetEntries
                        .Where(c => c.workItemId == tarefa.workItemId && c.sid == idUsuario).ToList();

                    //consulta o nome da tarefa
                    var task = ConsultarTarefa(tarefa.workItemId);
                    tarefa.NomeTarefa = task.Title;

                    //consulta as horas por dia
                    var horasPorDia = tarefaDoUsuario
                        .GroupBy(z => new { z.entryDate.Date })
                        .Select(x => new HoraPorDia { Data = x.Key.Date, Hora = x.Sum(y => y.hours) }).ToList();
                    tarefa.HorasPorDia = horasPorDia;

                    //consulta as horas para o tfs e timesheet
                    tarefa.TempoNoTFS = task.Fld10039 == null ? 0 : Convert.ToDecimal(task.Fld10039);
                    tarefa.TempoNaTimeSheet = tarefaDoUsuario.Sum(c => c.hours);
                }

                return tarefasAgrupadas;
            }
        }

        public static WorkItemsAre ConsultarTarefa(int id)
        {
            using (var db = new DBTfs_Softway())
            {
                return db.WorkItemsAre.First(c => c.ID == id);
            }
        }

        public static List<Constants> ConsultarListaUsuarios()
        {
            using (var db = new DBTfs_Softway())
            {
                var usuariosComTarefas = db.WorkItemsAre.Where(z => z.Fld10039 > 0).Select(c => c.PersonId).Distinct().ToList();
                return db.Constants.Where(c => usuariosComTarefas.Any(x => x == c.ConstID)).ToList();
            }
        }

        public static void Inserir(TimesheetEntries entidade)
        {
            using (var db = new DBTfs_Timesheet())
            {
                db.TimesheetEntries.AddObject(entidade);

                db.SaveChanges();
            }
        }

        public static void Atualizar(List<TimesheetEntries> entidade)
        {
            using (var db = new DBTfs_Timesheet())
            {
                foreach (var time in entidade)
                {
                    var apontamentos = db.TimesheetEntries.Where(c => c.workItemId == time.workItemId && c.sid == time.sid).ToList();
                    if (apontamentos.Count == 0)
                        db.TimesheetEntries.AddObject(time);

                    if (apontamentos.All(c => c.entryDate.Date != time.entryDate.Date) && time.hours == 0)
                        continue;

                    var horasPorDia = apontamentos
                        .GroupBy(z => new { z.entryDate.Date })
                        .Select(x => new HoraPorDia { Data = x.Key.Date, Hora = x.Sum(y => y.hours) }).ToList();

                    var apontamento = horasPorDia.FirstOrDefault(c => c.Data == time.entryDate.Date);
                    if (apontamentos.All(c => c.entryDate.Date != time.entryDate.Date) && time.hours > 0)
                    {
                        db.TimesheetEntries.AddObject(time);
                        continue;
                    }

                    time.hours = time.hours - apontamento.Hora;

                    db.TimesheetEntries.AddObject(time);
                }

                db.SaveChanges();
            }
        }
    }
}
